using Manager;
using UnityEngine;

namespace Player
{
    /// <summary>
    /// 相机控制类
    /// </summary>
    public class PlayerCameraController :MonoBehaviour
    {
        //输入系统
        private InputManager inputManager;
        //全局设置
        private GlobalSettingManager globalSettingManager;
        
        //位置组件
        private Transform trans;

        //跟随目标
        [SerializeField] private GameObject target;
        //枢轴
        [SerializeField] private GameObject pivot;
        
        
        //水平旋转角
        [SerializeField]
        private float horizontalRotation;
        //竖直旋转角
        [SerializeField]
        private float verticalRotation;
        
        private void Awake()
        {
            inputManager=InputManager.Instance;
            globalSettingManager=GlobalSettingManager.Instance;
            trans = transform;
        }


        //总更新
        public void HandleUpdate(float delta)
        {
            FollowTarget();
            RotateView(delta);

        }
        
        //跟随目标
        private void FollowTarget()
        {
            
            trans.position = target.transform.position;

        }

        
        //旋转视角
        private void RotateView(float delta)
        {
            
            horizontalRotation += inputManager.originViewRight * globalSettingManager.horizontalSensitivity*delta;
            //限制在-30到30°之间
            verticalRotation =Mathf.Clamp(verticalRotation - inputManager.originViewUp * globalSettingManager.verticalSensitivity*delta,PlayerConstant.MinViewAngle,PlayerConstant.MaxViewAngle);

            Vector3 rotation = Vector3.zero;
            
            rotation.y = horizontalRotation;
            
            trans.rotation=Quaternion.Euler(rotation);

            rotation = Vector3.zero;
            
            rotation.x = verticalRotation;

            pivot.transform.localRotation = Quaternion.Euler(rotation);




        }
        
        
    }
}